//
//  SecondTableViewCell.m
//  UI_19_数据请求model嵌套
//
//  Created by li cheng on 16/8/10.
//  Copyright © 2016年 li cheng. All rights reserved.
//

#import "SecondTableViewCell.h"
#import "SecondCollectionViewCell.h"
#import "ExtensionList.h"
#import "UIImageView+DownloadImage.h"
@interface SecondTableViewCell()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *extensionListArr;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@end
@implementation SecondTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}
#pragma mark - 初始化
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.layout = [[UICollectionViewFlowLayout alloc] init];
        self.collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:_layout];
        self.collectionView.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:_collectionView];
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
        [self.collectionView registerClass:[SecondCollectionViewCell class] forCellWithReuseIdentifier:@"SecondCollectionViewCell"];
        [self hotCarHandleDate];
    }
    return self;
}
#pragma mark - 获取数据
- (void)hotCarHandleDate{
    NSURLSession *urlSession = [NSURLSession sharedSession];
    NSURL *url = [NSURL URLWithString:@"http://adnewnc.app.autohome.com.cn/autov5.7.0/ad/infoad.ashx?appid=2&platform=1&version=6.2.0&networkid=0&adtype=1&provinceid=110000&lng=121.550888&lat=38.889660&pageindex=1&deviceid=d1ecb940123d620cb05a1b2b0a00da8bdd5e4fbe&idfa=981CD0F4-F1F0-4D0F-9C5E-58280F40E4B0&devicebrand=apple&devicemodel=iPhone&gps_city=210200&pageid=d1ecb940123d620cb05a1b2b0a00da8bdd5e4fbe1470913307169&isretry=0"];
    NSMutableURLRequest *URLRequest = [NSMutableURLRequest requestWithURL:url];
    URLRequest.HTTPMethod = @"GET";
    NSURLSessionDataTask *dataTask = [urlSession dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        dispatch_sync(dispatch_get_main_queue(), ^{
            NSError *error = nil;
            NSDictionary *extensionDictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
            NSArray *listArray = extensionDictionary[@"result"][@"list"];
            self.extensionListArr = [NSMutableArray array];
            for (NSDictionary *dic in listArray) {
                ExtensionList *extensionList = [[ExtensionList alloc] initWithDictionary:dic];
                [_extensionListArr addObject:extensionList];
            }
            [_collectionView reloadData];
        });
    }];
    [dataTask resume];
}
#pragma mark - 布局
- (void)layoutSubviews{
    [super layoutSubviews];
    _collectionView.frame = self.contentView.bounds;
    CGFloat width = self.contentView.bounds.size.width;
    CGFloat height = self.contentView.bounds.size.height;
    CGFloat edge = 5;
    self.layout.itemSize = CGSizeMake( (width - edge * 4) / 3 , height - edge * 2);
    self.layout.minimumLineSpacing = 5;
    self.layout.minimumInteritemSpacing = 5;
    self.layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _extensionListArr.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    SecondCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SecondCollectionViewCell" forIndexPath:indexPath];
    [cell.myImageView asyncDownloadImageWithURL:[_extensionListArr[indexPath.row] img] completionHandler:^(NSData *data, UIImage *image) {
        
    }];
    ExtensionList *extensionList = _extensionListArr[indexPath.row];
    cell.myLabel.text = extensionList.seriesname;
    return cell;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
